package flow;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Flow implements WritableComparable<Flow> {
    private String phone;
    private int upFlow;
    private int downFlow;
    private int sumFlow;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(int upFlow) {
        this.upFlow = upFlow;
    }

    public int getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(int downFlow) {
        this.downFlow = downFlow;
    }

    public int getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(int sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return phone + " "+ upFlow + " " + downFlow + " " + sumFlow;
    }


    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(phone);
        dataOutput.writeInt(upFlow);
        dataOutput.writeInt(downFlow);
        dataOutput.writeInt(sumFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.phone = dataInput.readUTF();
        this.upFlow = dataInput.readInt();
        this.downFlow = dataInput.readInt();
        this.sumFlow = dataInput.readInt();
    }

    @Override
    public int compareTo(Flow o) {
        // 排序规则如下：
        // 按照总流量降序排序
        // 如果总流量一致 那么按照上行流量升序排序
        // 如果上行流量和总流量都一致 那么按照下行流量排序
        if(this.sumFlow == o.getSumFlow()){
            if(this.upFlow == o.getUpFlow()){
                // 如果上行流量和总流量都一致 那么按照下行流量排序
                return this.downFlow > o.getDownFlow()?1:-1;
            }else {
                // 如果总流量一致 那么按照上行流量升序排序
                return this.upFlow > o.getUpFlow()?1:-1;
            }
        }else {
            // 按照总流量降序排序
            return this.sumFlow > o.getSumFlow()?-1:1;
        }
    }
}
